//
//  ZXXEmoticonToolBar.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/25.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit
// 设置button tag 使用
let tagNum = 2000
enum ZXXEmoticonToolBarButtonType: Int {
    //默认表情
    case Default = 0
    //emoji 表情
    case Emoji = 1
    // 浪小花表情
    case Lxh = 2
}

class ZXXEmoticonToolBar: UIView {
    ///按钮点击时被调用的闭包
    var ZXXEmoticonToolBarClosure:((type: ZXXEmoticonToolBarButtonType)->())?
    /// 记录当前选中的按钮
    var currentButton:UIButton = UIButton()
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置视图
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        backgroundColor = ThemeColor
        //compose_emotion_table_left_normal
        //compose_emotion_table_mid_selected
        
        //添加控件 
        let defaultButton = addChildButton("compose_emotion_table_left", title: "默认", type: .Default)
        let emojiButton = addChildButton("compose_emotion_table_mid", title: "Emoji", type: .Emoji)
        let lxhButton = addChildButton("compose_emotion_table_right", title: "浪小花", type: .Lxh)
        
        //添加约束
        defaultButton.snp_makeConstraints { (make) in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        //
        
    }
    
    func setEmoticonCurrentButton(section: Int){
        // 通过tag 获取指定的按钮
        let button = viewWithTag(section + tagNum) as! UIButton
        
        
        if button == currentButton{
            return
        }
        button.selected = true
        // 取消上次点击按钮的状态
        currentButton.selected = false
        // 保存这次点击的按钮
        currentButton = button
    }

    
    //  MARK: -  添加按钮的方法
    func addChildButton(imgName:String, title:String, type:ZXXEmoticonToolBarButtonType) -> UIButton {
        let button = UIButton()
        //添加点击事件
        button.tag = type.rawValue + tagNum
        button.addTarget(self, action: #selector(ZXXEmoticonToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        //设置button 背景图片
        button.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
        // 设置button文字颜色
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        addSubview(button)
        return button
        
    }
    
    //  MARK: -  按钮被点击时调用的方法
    @objc private func buttonClick(button: UIButton){
        //判断是同一个按钮就不变了
        if currentButton == button {
            return
        }
        //取消当前按钮的点击效果
        currentButton.selected = false
        //把选择的按钮改为选中状态
        button.selected = true
        //赋值当前的button 给currentButton
        currentButton = button
        
        //调用闭包把值传给keyboard
        ZXXEmoticonToolBarClosure?(type: ZXXEmoticonToolBarButtonType(rawValue: button.tag - tagNum)!)
    }

}


















